package com.demo.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.demo.domain.Admin;
import com.shopcart.domain.Store;
import com.vsd.domain.AreaAgent;


public class CoreAdminController {
	
	public final static String SESSION_ADMIN_USER="session_admin_user";
	
	public final static String LOGIN_SESSION_TIME="login_session_time";
	
	
	@InitBinder  
    public void initBinder(WebDataBinder binder) {  
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        dateFormat.setLenient(false);  
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));  
    } 
	@SuppressWarnings("unchecked")
	public String getSessionParameter(String name,HttpSession session) {
		Object obj = session.getAttribute(SESSION_ADMIN_USER);
		if (obj != null) {
			HashMap<String, String> map = (HashMap<String, String>) obj;
			return map.get(name);
		}
		return "";
	}
	@SuppressWarnings("unchecked")
	public void setSessionParameter(String name,String value,HttpSession session) {
		Object obj = session.getAttribute(SESSION_ADMIN_USER);
		if (obj != null) {
			HashMap<String, String> map = (HashMap<String, String>) obj;
			map.put(name, value);
		}
	}
	
	public void overwriteSessionParameter(Map<String, String> map,HttpSession session) {
		session.setAttribute(SESSION_ADMIN_USER, map);
	}
	
	public void initLoginTime(HttpSession session){
		session.setAttribute(LOGIN_SESSION_TIME, new Date());
	}
	
	public Object getCurrentUser(HttpServletRequest req){
		HttpSession session = req.getSession();
		if(session==null) return null;
		String id = getSessionParameter("id", session);
		if(StringUtils.isBlank(id)) return null;
		String roleCode = getSessionParameter("ROLE_CODE", session);
		if(roleCode.equals("ADMIN")||roleCode.equals("ADMINISTRATOR")){
			String username = getSessionParameter("username", session);
			Admin admin = new Admin();
			admin.setId(id);
			admin.setUsername(username);
			admin.setRoleCode(roleCode);
			return admin;
		}else if(roleCode.equals("store")){
			String username = getSessionParameter("username", session);
			Store store = new Store();
			store.setId(id);
			store.setAreaCode(getSessionParameter("aeraCode", session));
			store.setAccount(username);
			store.setName(getSessionParameter("name", session));
			store.setLogo(getSessionParameter("logo", session));
			return store;
		}else if(roleCode.equals("AGENT")){
			String username = getSessionParameter("username", session);
			AreaAgent store = new AreaAgent();
			store.setId(id);
			store.setAreaCode(getSessionParameter("aeraCode", session));
			store.setAccount(username);
			return store;
		}
		return null;
	}
	
	public void invalidateSession(HttpServletRequest req){
		req.getSession().setAttribute(SESSION_ADMIN_USER, null);
	}
	
	public String returnWithError(Model model,String errMsg){
		model.addAttribute("error",errMsg);
		return "login";
	}
	
}
